### Replication code for "Family Matters?"
### Summer 2018
### See the readme file for more details on what goes where in this replication package
### Contact Ariel White with questions: arwhi@mit.edu

library(data.table)
library(multiwayvcov)
library(lmtest)

#load main deidentified data 
load("felonycases_householdsmerged_deidentified.Rdata") #"smallneighborsfull2" 

#same code as in main file, but on different dataset (this yields Figure SI.6)

ed2012 <- as.Date("2012-11-06") #election day

smallneighborsfull2[,chargebefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==0,chargebefore:=1] 
smallneighborsfull2[,convbefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==1 & anyjail==0,convbefore:=1] 
smallneighborsfull2[,jailbefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==1 & anyjail==1,jailbefore:=1] 
smallneighborsfull2[,anyjail30:=0];smallneighborsfull2[totalsente >= 30, anyjail30:=1] #also look at longer jail sentences (>30 days)
smallneighborsfull2[,jail30before:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==1 & anyjail30==1,jail30before:=1] 

weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=17))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- smallneighborsfull2[firstcase < higher & firstcase > lower, ]
	#window <- window[!(firstcase > ed2012-3 & firstcase < ed2012+1), ] #can also drop the few days just prior to election,where it's hard to attribute treatment-- doesn't make much difference. 
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #estimate just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)
	ba1jail30 <- lm(vote2012 ~ jail30before, data=window[anyjail30==1,]); summary(ba1) #jail (30+days)
	ba1jail30.vcovCL<-cluster.vcov(ba1jail30, window[anyjail30==1,]$def_spn)
	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 
	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 
	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
	storage[i,14] <- est <- coeftest(ba1jail30, ba1jail30.vcovCL)[2,1]
	storage[i,15] <- p <- coeftest(ba1jail30, ba1jail30.vcovCL)[2,4]
	err<- coeftest(ba1jail30, ba1jail30.vcovCL)[2,2]; dof <- ba1jail30$df
	storage[i,16] <- est + -1*err*qt(0.975, dof) 
	storage[i,17] <- est + 1*err*qt(0.975, dof)
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI",  "jail30_est", "jail30_p", "jail30_lowCI", "jail30_highCI")
storage$row <- 1:nrow(storage)

#make plots for Figure SI7
pdf("beforeafter_justcharge_windows40_felons.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Felony Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40_felons.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail_windows40_felons.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Incarceration on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()


